<!-- 商品详情页面 -->
<template>
<transition>
  <div class="food-wrap" v-show="showFlag">
    <div class="detail">
      <div class="image-wrap">
        <img :src="food.image" class="image"/>
        <div class="back" @click="hide">
          <i class="icon-arrow_lift"></i>
        </div>
      </div>
      <div class="content">
        <h2 class="name">{{ food.name }}</h2>
        <div class="extra">
          <span>月售{{ food.sellCount }}份</span>
          <span>好评率{{ food.rating }}%</span>
        </div>
        <div class="price">
          <span class="new-price">￥{{ food.price }}</span><span class="old-price" v-show="food.oldPrice">￥{{ food.oldPrice }}</span>
        </div>
        <div class="add">
          <cartcontrol :food="food" v-if="$store.getters.FoodCount[food.id]>0"></cartcontrol>
          <mt-button type="primary" class="btn" @click="addToShopCar(food)" v-else>加入购物车</mt-button>
        </div>
      </div>
    </div>
    <split v-show="food.info"></split>
    <div class="introduce" v-show="food.info">
      <h3 class="title">商品介绍</h3>
      <p class="info">{{ food.info }}</p>
    </div>
    <split></split>
    <div class="rating">
      <h1 class="title">商品评价</h1>
      <ratingselect :ratings="food.ratings" :select-type="selectType" :desc="desc"
      :only-content="onlyContent" @toggleSelected="toggleSelected" @toggleOnlyContent="toggleOnlyContent">
      </ratingselect>
      <div class="content">
          <ul>
            <li class="item border-1px" v-for="item in food.ratings" :key="item.rateTime"
            v-show="isRatingsShow(item.rateType,item.text)">
              <div class="info">
                <div class="info-left">
                  <span class="date">{{ item.rateTime | formatDate }}</span>
                </div>
                <div class="info-right">
                  <span class="idcard">{{ item.username }}</span>
                  <img :src="item.avatar" class="avatar"/>
                </div>
              </div>
              <div class="comment-wrap">
                <i class="icon-thumb_up comment-icon" v-if="item.rateType === 0"></i>
                <i class="icon-thumb_down comment-icon" v-else></i>
                <span class="comment">{{ item.text }}</span>
              </div>
            </li>
          </ul>
      </div>
    </div>
  </div>
</transition>
</template>
<script>
import cartcontrol from "../cartcontrol/cartcontrol";
import split from "../split/split";
import ratingselect from "../ratingselect/ratingselect";
// const POSITIVE = 0;
// const NEGETIVE = 1;
// const ALL = 2;
export default {
  data() {
    return {
      showFlag: false,
      desc: {
        all: "全部",
        positive: "推荐",
        negetive: "吐槽"
      },
      onlyContent: true,
      selectType: 0
    };
  },
  props: ["food"],
  methods: {
    // 显示组件
    show() {
      this.showFlag = true;
      // 每次进入一个新的food页时，将selectType和onlyContent 重置一遍；
      this.selectType = 2;
      this.onlyContent = false;
    },
    hide() {
      this.showFlag = false;
    },
    addToShopCar(f) {
      let addFood = {
        id: f.id,
        name: f.name,
        price: f.price,
        count: 1
      };
      this.$store.commit("increase", addFood);
    },
    // 提供给ratingselect组件，改变selectType的值
    toggleSelected(type) {
      this.selectType = type;
    },
    // 提供给ratingselect组件，改变onlyContent的值
    toggleOnlyContent() {
      this.onlyContent = !this.onlyContent;
    },
    // 判断评论条目是否显示
    isRatingsShow(rateType, text) {
      if (this.onlyContent && text.length === 0) {
        return false;
      }
      if (rateType === this.selectType || this.selectType === 2) {
        return true;
      }
    }
  },
  components: {
    cartcontrol,
    split,
    ratingselect
  }
};
</script>
<style lang='less' scoped>
@import "../../common/css/common.less";
.v-enter,
.v-leave-to {
  opacity: 0;
}
.food-wrap {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 46px;
  width: 100%;
  z-index: 1;
  overflow: auto;
  background-color: white;
  transition: all .8s;
  &::-webkit-scrollbar {
    display: none;
  }
  .detail {
    background-color: white;
    // 利用padding或margin撑开元素，做到自适应，并且加载时不会闪烁
    .image-wrap {
      width: 100%;
      position: relative;
      overflow: hidden;
      &::after {
        content: "";
        display: block;
        margin-top: 100%;
      }
      .image {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
      }
      .back {
        position: absolute;
        top: 10px;
        left: 0;
        .icon-arrow_lift {
          display: block;
          padding: 10px;
          font-size: 20px;
          color: white;
        }
      }
    }
    .content {
      position: relative;
      padding: 18px;
      .name {
        margin-bottom: 8px;
        .font-style(14px,14px);
        color: rgb(7, 17, 27);
      }
      .extra {
        margin-bottom: 18px;
        .font-style(10px,10px);
        color: rgb(147, 153, 159);
        &:first-child {
          margin-right: 12px;
        }
      }
      .price {
        .new-price {
          margin-right: 8px;
          .font-style(14px,24px,700);
          color: rgb(240, 20, 20);
        }
        .old-price {
          .font-style(10px,24px,700);
          color: rgb(147, 153, 159);
          text-decoration: line-through;
        }
      }
      .add {
        position: absolute;
        right: 18px;
        bottom: 18px;
        font-size: 0;
        .btn {
          width: 84px;
          height: 24px;
          border-radius: 12px;
          .font-style(10px,12px);
        }
      }
    }
  }
  .introduce {
    padding: 18px;
    background-color: white;
    .title {
      margin-bottom: 6px;
      .font-style(14px,14px);
      color: rgb(7, 17, 27);
    }
    .info {
      padding: 0 8px;
      .font-style(12px,24px,200);
      color: rgb(77, 85, 93);
    }
  }
  .rating {
    padding-top: 18px;
    .title {
      margin-left: 18px;
      margin-bottom: 6px;
      .font-style(14px,14px);
      color: rgb(7, 17, 27);
    }
    .content {
      .item {
        margin: 0 18px;
        padding: 16px 0;
        .border-1px();
        .info {
          margin-bottom: 6px;
          color: rgb(147, 153, 159);
          display: flex;
          justify-content: space-between;
          .info-left {
            .date {
              display: inline-block;
              .font-style(10px,12px);
            }
          }
          .info-right {
            .idcard {
              display: inline-block;
              .font-style(10px,12px);
              margin-right: 6px;
            }
            .avatar {
              width: 12px;
              height: 12px;
              border-radius: 50%;
              background-color: red;
              vertical-align: middle;
            }
          }
        }
        .comment-wrap {
          .comment-icon {
            margin-right: 4px;
            .font-style(12px,24px);
            &.icon-thumb_down {
              color: rgb(147, 153, 159);
            }
            &.icon-thumb_up {
              color: rgb(0, 160, 220);
            }
          }
          .comment {
            .font-style(12px,16px);
            color: rgb(7, 17, 27);
          }
        }
      }
    }
  }
}
</style>